let app = getApp()
import utils from '../../utils/util'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        loadingHidden: true,
        formData: {},
        hostForm: {},
        wordsList: [],
        hostTypeIsStu: '',
        hostId: '',
        messageId: '',
        backUrl: '',
        job: {
            jobName: '',
            jobTime: '',
            jobAddress: '',
            jobContent: '',
        },
        words: '',
        userType: -1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log('当前角色：' + app.globalData.userType)
        utils.mask(this)
        let messageId = options.id
        this.setData({
            // messageId: options.id,
            messageId: messageId,
            backUrl: options.backUrl,
            userType: app.globalData.userType
        })
        this.getDataInfo(messageId)
        this.getWordsInfo(messageId)
    },

    /**
     * 留言
     */
    getWordsInfo(id) {
        let that = this
        wx.request({
            url: app.globalData.serviceUrl + '/words/list',
            data: {
                messageId: id
            },
            success: function (res) {
                let tempData = res.data
                if (tempData.success) {
                    that.setData({
                        wordsList: tempData.data,
                    })
                }
            }
        })
    },
    /**
     * 主体信息
     * @param id
     */
    getDataInfo(id) {
        let that = this
        wx.request({
            url: app.globalData.serviceUrl + '/message/query',
            data: {
                id: id
            },
            success: function (res) {
                let tempData = res.data
                if (tempData.success) {
                    that.setData({
                        formData: tempData.data,
                    })
                    if (tempData.data.type === '3'){
                        that.setData({
                            job: JSON.parse(tempData.data.content)
                        })
                    }
                    if (tempData.data.studentId) {
                        that.setData({
                            // 学生
                            hostTypeIsStu: true,
                            hostId: tempData.data.studentId,
                        })
                    } else {
                        that.setData({
                            // 商家
                            hostTypeIsStu: false,
                            hostId: tempData.data.shopId
                        })
                    }
                    // that.loadUserInfo()
                } else {
                    utils.toast('获取详情失败')
                }
                utils.removeMask(that)
            },
            fail: function () {
                utils.removeMask(that)
                utils.toast('获取详情失败')
            }
        })
    },
    sendWords(){
        let that = this
        console.log(this.data.words)
        if (that.data.words.trim().length >= 2){
            utils.mask(that)
            wx.request({
                url: app.globalData.serviceUrl + '/words/save',
                data: {
                    hostId: app.globalData.userInfo.id,
                    hostName: app.globalData.userInfo.nickName || app.globalData.userInfo.shopName,
                    hostType: app.globalData.userType,
                    hostImgUrl: app.globalData.userInfo.avatarUrl || app.globalData.userInfo.imgUrl || '../../resources/user.png',
                    messageId: that.data.formData.id,
                    content: that.data.words
                },
                success: function (res) {
                    let tempData = res.data
                    if (tempData.success) {
                        utils.toast('发布成功')
                        that.getWordsInfo(that.data.formData.id)
                        that.setData({
                            words: ''
                        })
                    } else {
                        utils.toast(tempData.msg)
                    }
                    utils.removeMask(that)
                },
                fail: function () {
                    utils.removeMask(that)
                    utils.toast('请求失败')
                }
            })
        } else {
            utils.toast('留言不得低于2个字符')
        }


    },
    watchWords(e){
        this.setData({
            words: e.detail.value
        })
    },

    showHostInfo(e) {
        let url;
        let hid = e.currentTarget.dataset.hid;
        let htype = e.currentTarget.dataset.htype;
        console.log(hid, "====", htype)
        if (hid && htype) {
            url = '/pages/hostinfo/hostinfo?isStu=' + (htype === '1') + '&id=' + hid
        } else {
            url = '/pages/hostinfo/hostinfo?isStu=' + this.data.hostTypeIsStu + '&id=' + this.data.hostId
        }
        console.log(url)
        utils.redirectToPage(url, this)
    },

    saveCollect() {
        let that = this
        let id = this.data.formData.id
        utils.mask(that)
        wx.request({
            url: app.globalData.serviceUrl + '/collection/save',
            data: {
                messageId: id,
                studentId: app.globalData.userInfo.id
            },
            success: function (res) {
                let tempData = res.data
                if (tempData.success) {
                    utils.toast('收藏成功')
                } else {
                    utils.toast(tempData.msg)
                }
                utils.removeMask(that)
            },
            fail: function () {
                utils.removeMask(that)
                utils.toast('请求失败')
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
